package com.example.g2daylx2.fg.one

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.lib_base.net.ApiService
import com.example.lib_base.net.entity.CarEntityResultItem

class CarDataSource(val apiService: ApiService,val para:String):PagingSource<Int,CarEntityResultItem>() {
    override fun getRefreshKey(state: PagingState<Int, CarEntityResultItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CarEntityResultItem> {
       try {
           val page = params.key ?: 1
           val res = apiService.car(para)
           val prePage = if (page!=1)page-1 else null
           val nexPage = if (res.data.size!=0) page+1 else null
           return LoadResult.Page(res.data,prePage,nexPage)
       }catch (e:Exception){
           return LoadResult.Error(e)
       }
    }
}